package com.youlu.campus.service.notify;

import com.youlu.campus.entity.kuaidi100.KuaiDi100PollMapBodyDTO;
import com.youlu.campus.entity.kuaidi100.KuaiDi100PushCompensate;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class KuaiDi100PushCompensateService {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private KuaiDi100Service kuaiDi100Service;

    @Autowired
    private MapperFacade mapperFacade;

    public void job(){
        log.info("starting running");
        Integer pageNo =1;
        Integer pageSize =1000;
        while (true){
            log.info(":<<<< running pageNo:{}",pageNo);
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            Query query = new Query();
            query.addCriteria(Criteria.where("status").is(1));
            List<KuaiDi100PushCompensate> list = this.mongoTemplate.find(query.with(pageRequest), KuaiDi100PushCompensate.class);
            if(CollectionUtils.isEmpty(list)){
                log.info("starting running end break");
                break;
            }
            list.stream().forEach(kuaiDi100PushCompensate -> {
                KuaiDi100PollMapBodyDTO dto =
                        KuaiDi100PollMapBodyDTO.builder().
                                number(kuaiDi100PushCompensate.getNumber()).
                                to(kuaiDi100PushCompensate.getTo()).
                                orderNo(kuaiDi100PushCompensate.getOrderNo())
                                .subOrderNo(kuaiDi100PushCompensate.getSubOrderNo())
                                .phone(kuaiDi100PushCompensate.getPhone())
                                .from(kuaiDi100PushCompensate.getFrom())
                                .logisticsCompany(kuaiDi100PushCompensate.getLogisticsCompany()).
                                compensatePushId(kuaiDi100PushCompensate.getId()).
                                build();
//                KuaiDi100PollMapBodyDTO dto = mapperFacade.map(kuaiDi100PushCompensate, KuaiDi100PollMapBodyDTO.class);
                dto.setCompensatePushId(kuaiDi100PushCompensate.getId());
                kuaiDi100Service.pushRollMap(dto);
            });
            pageNo++;
        }
        log.info("：>>> starting running end");
    }
}
